package com.beetlestech.nfe.main.biz.com.impl;

import java.util.ArrayList;
import java.util.List;

import com.beetlestech.nfe.fw.core.biz.AbstractDBLogic;
import com.beetlestech.nfe.fw.core.util.CollectionUtil;
import com.beetlestech.nfe.main.biz.com.HbCourseEvaluation;
import com.beetlestech.nfe.main.biz.com.dto.CourseEvaluationDTO;
import com.beetlestech.nfe.main.biz.com.dto.EvaluationInfoDTO;
import com.beetlestech.nfe.main.biz.com.dto.VcourseevaluationInfoDTO;
import com.beetlestech.nfe.main.biz.constant.CommonConstant;
import com.beetlestech.nfe.main.biz.constant.CtgCodeConstant;
import com.beetlestech.nfe.main.biz.constant.HbMessageCode;
import com.beetlestech.nfe.main.db.constant.SQLConstant;
import com.beetlestech.nfe.main.db.tbl.CourseevaluationDTO;
import com.beetlestech.nfe.main.db.tbl.MstcourseDTO;
import com.beetlestech.nfe.main.db.view.VcourseevaluationDTO;
import com.beetlestech.nfe.main.db.view.VevaluationDTO;


public class HbCourseEvaluationImpl extends AbstractDBLogic implements HbCourseEvaluation, CtgCodeConstant, HbMessageCode, CommonConstant, SQLConstant {
	protected static final String SH_INFO = "课程评价信息表";

	public CourseEvaluationDTO getCourseEvaluation(Integer courseID, Integer rowStar, Integer rowEnd){
		CourseEvaluationDTO courseEvaluation = new CourseEvaluationDTO();
		MstcourseDTO courseDTO = new MstcourseDTO();
		courseDTO.setcourseid(courseID);
		courseDTO = queryDAO.queryForObject(MstcourseDTO.EQUAL_SEARCH, courseDTO);
		
		if(courseDTO==null){
			createApplicationException(M10010WS);
		}
		
		courseEvaluation.setCourseId(courseID);
		courseEvaluation.setName(courseDTO.getname());
		
		VevaluationDTO searchDTO = new VevaluationDTO();
		searchDTO.setcourseid(courseID);
		List<VevaluationDTO> evaluations = queryDAO.queryForObjectList(VevaluationDTO.EQUAL_SEARCH, searchDTO);
		if (CollectionUtil.isEmpty(evaluations)) {
			createApplicationException(M10010WS);
		}
		
		List<EvaluationInfoDTO> lst = new ArrayList<EvaluationInfoDTO>();
		
		Integer irow = 0;
		for(VevaluationDTO item: evaluations){
			irow ++;
			if(rowStar == null || rowEnd == null || (irow >= rowStar && irow <= rowEnd)){
				lst.add(convertToEvaluation(item));
			}
			
			if(rowEnd != null && irow > rowEnd){
				break;
			}
		}
	
		courseEvaluation.setEvaluationInfoArray(lst.toArray(new EvaluationInfoDTO[lst.size()]));
		return courseEvaluation;
	}
	
	public void insertCourseEvaluation(Integer studentID, Integer courseID, String content){
		CourseevaluationDTO courseevaluationDTO = new CourseevaluationDTO();
		courseevaluationDTO.setstudentid(studentID);
		courseevaluationDTO.setcourseid(courseID);
		courseevaluationDTO.setcontent(content);
		updateDAO.updateOne(CourseevaluationDTO.INSERT, courseevaluationDTO, M10190WS, SH_INFO);
	}
	
	private EvaluationInfoDTO convertToEvaluation(VevaluationDTO item) {
		EvaluationInfoDTO evaluationInfo = new EvaluationInfoDTO();
		evaluationInfo.setUserName(item.getname());
		evaluationInfo.setPath(item.getpath());
		evaluationInfo.setContent(item.getcontent());
		evaluationInfo.setUpdateDate(item.getlastdate());
		evaluationInfo.setUpdateTime(item.getlasttime());
		return evaluationInfo;
	}

	@Override
	public VcourseevaluationInfoDTO[] getCourseevaluationList(Integer courseId) {
		List<VcourseevaluationInfoDTO> courseEvaluationList = new ArrayList<VcourseevaluationInfoDTO>();
		VcourseevaluationDTO vcourseevaluationDTO=new VcourseevaluationDTO();
		if(courseId!=0){
			vcourseevaluationDTO.setcourseid(courseId);
		}
		List<VcourseevaluationDTO> courseevaluationListDb=queryDAO.queryForObjectList(VcourseevaluationDTO.EQUAL_SEARCH, vcourseevaluationDTO);
		if(courseevaluationListDb==null){
			createApplicationException("errMsg", "没找到要查询的信息");
		}else{
			for(VcourseevaluationDTO item:courseevaluationListDb){
				courseEvaluationList.add(convertToVcourseevaluation(item));
			}
		}
		
		return courseEvaluationList.toArray(new VcourseevaluationInfoDTO[courseEvaluationList.size()]);
		
		
	}
	private VcourseevaluationInfoDTO convertToVcourseevaluation(VcourseevaluationDTO item){
		VcourseevaluationInfoDTO vcourseevaluationInfoDTO = new VcourseevaluationInfoDTO();
		vcourseevaluationInfoDTO.setcontent(item.getcontent());
		//vcourseevaluationInfoDTO.setcourseid(item.getcourseid());
		vcourseevaluationInfoDTO.setcoursename(item.getcoursename());
		vcourseevaluationInfoDTO.setevaluationdate(item.getevaluationdate());
		vcourseevaluationInfoDTO.setevaluationid(item.getevaluationid());
		vcourseevaluationInfoDTO.setevaluationtime(item.getevaluationtime());
		vcourseevaluationInfoDTO.setisshow(item.getisshow());
		vcourseevaluationInfoDTO.setstudentname(item.getstudentname());
		return vcourseevaluationInfoDTO;
	}

}
